# This code is so you can run the samples without installing the package
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..'))

# stdlib


#pyglet
from pyglet.gl import *

# cocos2d related
import cocos
from cocos.layer import *
# from cocos.layer import Layer, ColorLayer
from cocos.scene import Scene
from cocos.director import director
from cocos.actions import *
from cocos.tiles import *

# tetrico related
from gamectrl import *
from gamemodel import *
from constants import *

__all__ = ['get_newgame']

class GameView( Layer ):

    def __init__(self, model):
        super(GameView,self).__init__()

        width, height = director.get_window_size()

        self.duration = 8

        self.position = ( 0, 0 )

        self.model = model

        # self.transform_anchor = ( COLUMNS*SQUARE_SIZE /2, ROWS * SQUARE_SIZE/2)

        # background layer to delimit the pieces visually
        hudLayer = ColorLayer( 0,0,0,255, width = 608, height = 32)
        hudLayer.position = (192,736)
        bl = ColorLayer( 0,0,0,0, width = 608, height = 704)
        bl.position = (192,32)
        label = cocos.text.Label('Score',
            font_name='Times New Roman',
            font_size=16,
            anchor_x='left', anchor_y='bottom',
            )
        label.position = (192,736)
        self.add(label, z=1)
        self.add(hudLayer, z=0)
        self.add(bl, z=0)
        
        # No necesito mas cargar el mapa aca, ya lo tiene el model!!!
        #self.tmx_resource = load_tmx('niveles.tmx')
        self.scroller = layer.ScrollingManager()
        # levanto las capas del tmx desde el model
        nivel_1     = self.model.nivel_1
        pastis     = self.model.pasti_map

        # Agrego las capas al manager
        # con el mismo orden que se que tienen en el tmx!
        #self.scroller.add(items_1, z=0)
        self.scroller.add(pastis, z=1)
        self.scroller.add(nivel_1, z=1)
        # agrego una capa de color a la escena
        self.add( self.scroller, z=0 )

        
        
        self.model.push_handlers( self.on_move_bicho,  self.on_stop_bicho)

    def on_enter(self):
        super(GameView,self).on_enter()

    def on_exit(self):
        super(GameView,self).on_exit()

    def on_move_bicho(self ):
        #print('moved bicho!!')
        return True

    def on_stop_bicho(self ):
        #print('STOP bicho!!')
        return True      

    def draw( self ):
        '''draw the map and the block'''

        #glPushMatrix()
        #self.transform()

        self.model.update()

        if self.model.bicho:
            self.model.bicho.draw()
            self.model.sombra.draw()
            self.model.rapido.draw()
            self.model.miedoso.draw()
            self.model.tonto.draw()            

        
        # Ahora usando la clase nueva no hay que hacer ni un for aca en el gameview

        #self.model.bigPellets.draw()

        #self.model.smallPellets.draw()
        
        #self.model.lifes.draw()
        
        #glPopMatrix()


def get_newgame():
    '''returns the game scene'''
    scene = Scene()

    # model
    model = GameModel()

    # controller
    ctrl = GameCtrl( model )
    ctrl.resume_controller()
    # view
    view = GameView( model )
    # set controller in model
    model.set_controller( ctrl )
    
    # add controller
    scene.add( ctrl, z=1, name="controller" )

    # add view
    scene.add( view, z=2, name="view" )

    return scene
